/*
 * Copyright (C) 2024 ~ 2025 Deepin Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "widgets/titlebuttonitem.h"
#include "widgets/titledslideritem.h"
#include "widgets/dccslider.h"
#include "compixmap.h"
#include "usbrecordsdelegate.h"
#include "usbstoragesecuritymodel.h"
#include "usbwhitelistdlg.h"
#include "common/usbmanagercomm.h"
#include "widgets/usbstorageitemrefreshmodel.h"
#include "widgets/hframe1.h"

#include <QTableView>
#include <QCommandLinkButton>
#include <QLabel>
//#include <DSearchEdit>
#include <QLineEdit>

#include <QVBoxLayout>
#include <QStandardItemModel>
#include <QSettings>
#include <QItemDelegate>

using namespace def::widgets;

class USBStorageSecurityWidget : public QWidget
{
    Q_OBJECT
public:
    explicit USBStorageSecurityWidget(USBStorageSecurityModel *model, QWidget *parent = nullptr);
    ~USBStorageSecurityWidget();

private:
    QHBoxLayout *initTable();

    void initConnect();
    QWidget *initConnectionRecordTableWgt();

    // 将USB连接记录数据进行添加
    void appendUsbConnectionLog(USBConnectionInfo info);
    // 响应按钮触发 清除所有连接记录
    Q_SLOT void confirmClearUsbConnectionLog();

    //根据状态更新usb限制模式项的按钮菜单
    Q_SLOT void updateUsbLimitModeItem();
    // 清理界面中超过30天的日志
    void cleanUsbConnectionLog();
    // 通过usb连接信息获得数据项
    QStandardItem *createDataItemFromInfo(const USBConnectionInfo &info);

    // 使能所有控件
    void enableAllWidgets(bool enable);

    // 重新加载USB 连接记录列表
    void reloadUSBConnList(const USBConnectionInfoList &list);

    // 更新界面
    void updateUI();

public Q_SLOTS:
    void selectLimitationModel(QAction *action);
    void onRecvNeedShowRecords(const USBConnectionInfoList &list); // 接收到所有需要显示的USB设备连接记录信息
    void onReceiveUsbInfo(USBConnectionInfo info); // 接收USB设备连接记录信息
    void onOpenWhitelist(); // 打开白名单
    void onShowDetailsDlg(const USBConnectionInfo &info); // 显示详情页
    void onWhitelistStrategyChanged(int type, const USBConnectionInfo &info); // 白名单添加操作  && 白名单移除操作
    void removeWhitelist(const USBConnectionInfo &info); // 完成信号->移除白名单
    void addWhitelist(const USBConnectionInfo &info); // 完成信号->添加白名单
    void showAddWhitelistTip();
    void clearAllDevRecords(); // 清除所有界面 连接记录
    void exportAllRecords(); // 导出全部连接记录信息
    void matchConetentBySearch(const QString &text); // 根据文字匹配搜索框输入的文字
    void updateUIStatus(); // 界面是否启用状态刷新

    void enableAllWidgetsTrue();
    void confirmClearUsbConnectionLogHelp(int index, QString);
    void selectLimitationModelHelp(int index, QString);

    void onWhitelistStrategyChangedHelp(int index, const QString &);
private:
    USBStorageSecurityModel *m_usbStorageSecurityModel; // 数据类

    TitleButtonItem *m_usbLimitModeItem; // Item标题
    QLabel *m_usbLimitModeItemButtonLabel; // 限制模式选择Item显示label
    QMenu *m_usbLimitModeMenu; // 模式选择菜单
    QLineEdit *m_searchEdit; //搜索框

    DefenderTableHeaderView *m_headerDelegate; // 表头代理类
    QWidget *m_connectionRecordTableWgt; // 表格控件
    QTableView *m_connectionRecordTable;
    USBStorageItemRefreshModel *m_connectionRecordTableModel;

    QCommandLinkButton *m_whiteListBtn; // 白名单按钮
    QCommandLinkButton *m_clearLogBtn; // 清除记录按钮
    QCommandLinkButton *m_exportBtn; // 导出记录按钮
    USBWhiteListDlg *m_whitelistDialog; // 白名单弹窗
    USBDeviceDetailsDlg *m_USBDeviceDetails; // USB详情页弹窗

    USBRecordsDelegate *m_USBRecordsDelegate; // 代理类

    bool m_needShowNoSearchResultTip; // 是否需要显示无搜索结果提示
    QLabel *m_blankTip; // 无搜索结果提示
    HFrame1 *m_blankArea; // 无策略时文字区域
};
